# -*- codeing = utf-8 -*-
# @Time : 2021/2/26 9:34
# @Atuthor: 朱朱
# @File: 106运算符相关的魔法方法.py
# @Software: PyCharm
class Person:
    def __init__(self,name, age):
        self.name = name
        self.age =age

    def __eq__(self, other):
        return self.name==other.name and self.age==other.age

    def __ne__(self, other):
        return '!='

    def __gt__(self, other):
        return self.age > other.age

    def __ge__(self, other):
        return '>='
    def __lt__(self, other):
        return '<'
    def __le__(self, other):
        return '<='

    def __add__(self, other):
        return self.age+ other.age
    def __sub__(self, other):
        return self.age-other
    def __mul__(self, other):
        return self.age*other
    def __truediv__(self, other):
        return '/'
    def __pow__(self, power, modulo=None):
        return '**'

    def __str__(self):
        return 'hello'
    def __int__(self):
        return 20
    def __float__(self):
        return 100.5

p1 = Person('zhangsan', 18)
p2 = Person('zhangsan', 18)
p3 = Person('lisi', 19)
print(p1 is p2)  # False 调用is,比较内存地址,所以不等
print(p1 == p2) # True 调用eq方法,比较内容

print(p1 != p2) # 调用ne方法
print(p1 > p2)

print(p1 + p2)
print(p1 - 2)
print(p1 * 2)

# str()将对象转换成为字符串，会自动调用 __str__ 方法
# 1. str()  2. 打印对象也会调用
print(p1)
x = str(p1)  # 转换成为字符串。默认会转换成为类型+内存地址
print(x)

# int()  ==> 调用对象的 __int__ 方法
print(int(p1))
print(float(p1))

print(int(p1))  # 只调用了__int__，没有调用 __str__
print(p1)  # 调用 __str__